//
//  WTLoginViewController.m
//  OneDay
//
//  Created by lanou on 16/1/19.
//  Copyright © 2016年 小明. All rights reserved.
//

#import "WTLoginViewController.h"
#import "WTLoginView.h"
#import "WTRegisterViewController.h"
#import "WTRequestManager.h"
#import "WTInformationHandle.h"

@interface WTLoginViewController ()

@property (nonatomic, strong) WTLoginView * loginView;
@end

@implementation WTLoginViewController




- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"登录";
    self.tabBarController.tabBar.hidden = YES;
    self.view.backgroundColor = [UIColor whiteColor];
    
    _loginView = [[WTLoginView alloc]initWithFrame:CGRectMake(0, 64, kScreenWidth, kScreenHeight - 64)];
    [self.view addSubview:_loginView];
    
    [self addLoginView];
    
    UIBarButtonItem * leftButton = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"m-fanhui"] style:(UIBarButtonItemStylePlain) target:self action:@selector(leftButtonClick:)];
    self.navigationItem.leftBarButtonItem = leftButton;
    
    // Do any additional setup after loading the view.
}

// 返回按钮
- (void)leftButtonClick:(UIButton *)sender{

    [self.navigationController popViewControllerAnimated:YES];
}
// 给LoginView上面的控件赋值
- (void)addLoginView{

    _loginView.userImageV.image = [UIImage imageNamed:@"m-yonghu"];
    _loginView.userTF.placeholder = @"用户名/手机号";
    _loginView.passwordImageV.image = [UIImage imageNamed:@"m-mima1"];
    _loginView.passwordTF.placeholder = @"密码";
    
    [_loginView.loginButton setTitle:@"登录" forState:(UIControlStateNormal)];
    [_loginView.loginButton addTarget:self action:@selector(loginButtonClick:) forControlEvents:(UIControlEventTouchUpInside)];
    
    [_loginView.registerButton setTitle:@"注册" forState:(UIControlStateNormal)];
    [_loginView.registerButton addTarget:self action:@selector(registerButtonClick:) forControlEvents:(UIControlEventTouchUpInside)];
}



// 登录
- (void)loginButtonClick:(UIButton *)sender{

  
    
    NSMutableDictionary * dic = [NSMutableDictionary dictionary];
    dic[@"act"] = @"login";
    dic[@"uid"] = _loginView.userTF.text;
    dic[@"pwd"] = _loginView.passwordTF.text;
    
    
    // 登录的请求网址,会返回给我们登录的信息
    [WTRequestManager requestWithUrlString:@"http://bbs.mier123.com/api/2.0.3/logreg_json.php?plat=android&proct=mierapp&versioncode=20150807&apiCode=4" parDic:dic method:POST success:^(NSData *data) {
        NSDictionary * dataDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        
        NSLog(@"%@",dataDic);
        // 单例里面的属性赋值
        WTInformationHandle * handle = [WTInformationHandle shareInformationHandle];
        [handle setValuesForKeysWithDictionary:dataDic];
        
        if ([[WTInformationHandle shareInformationHandle].error isEqualToString:@"1"]) {
            
            
            // 错误原因
            NSString * errorInfo = dataDic[@"msg"];
            if ([errorInfo isEqualToString:@"pwd is null"]) {
                errorInfo = @"密码为空";
            }
            
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"登录失败" message:errorInfo preferredStyle:(UIAlertControllerStyleAlert)];
            UIAlertAction * okAction = [UIAlertAction actionWithTitle:@"确认" style:(UIAlertActionStyleDefault) handler:nil];
            [alert addAction:okAction];
            [self presentViewController:alert animated:YES completion:nil];
        }else{
          
            // 存储用户信息
            NSUserDefaults * user =  [NSUserDefaults standardUserDefaults];
//            [user setValue:_loginView.userTF.text forKey:@"userName"];
            
            [user setValue:dataDic[@"nickname"] forKey:@"userName"];

            [user setValue:_loginView.passwordTF.text forKey:@"password"];
            [user setValue:handle.userImg forKey:@"userImg"];
            [user setValue:dataDic[@"id"] forKey:@"uid"];
            [user synchronize];
            
            
            [self.navigationController popViewControllerAnimated:YES];
            
        }
    } error:^(NSError *error) {
        
        
        
    }];
}

// 注册
- (void)registerButtonClick:(UIButton *)sender{

    WTRegisterViewController * registerVC = [[WTRegisterViewController alloc]init];
    [self.navigationController pushViewController:registerVC animated:YES];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
